using System;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class Validation : MissionPlugin
    {
        [Package(Direction.Input, "Defines the data you like to validate against the validation rules.")]
        enum Data2Validate
        { }
        
        [Package(Direction.Input, "Defines the validation rules you will expect.")]
        enum ValidationRules
        { }

        [Plugin(PluginDevStatus.Production, "Validate a given package against the expected data. This plugin is an important part of unit-testing.")]
        public Validation()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }


        protected override void Work()
        {
            int row = 0;
            MissionPluginPackage dataToValidate = this.GetPackageByName(typeof(Data2Validate));

            DataTableReader extractRulesPackage = this.GetDataReader(typeof(ValidationRules));
            while (extractRulesPackage.Read() && row < dataToValidate.Rows.Count)
            {
                string errorMessage = "";

                foreach (MissionPluginPackageColumn col in dataToValidate.Columns)
                {
                    if (dataToValidate.Rows[row][col.ColumnName].ToString() != extractRulesPackage[col.ColumnName].ToString())
                    {
                        errorMessage += String.Format("Validation failed on row {1}: '{2}' was expected instead of '{0}' | ", dataToValidate.Rows[row][col.ColumnName].ToString(), row.ToString(), extractRulesPackage[col.ColumnName].ToString());
                    }
                }

                if (true == String.IsNullOrEmpty(errorMessage))
                {
                    this.AddNewLoggingMessage(typeof(ValidationRules), String.Format("row {0} is valid!", row.ToString()));
                }
                else
                {
                    this.AddNewLoggingError(errorMessage);
                }
                row++;
            }
        }
    }
}
